﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using OpenCvSharp;

public class CaptureForm2 : Form
{
    private System.Drawing.Point startPoint;
    private System.Drawing.Point endPoint;
    private bool isDragging;
    private Rectangle selectionRect;
    private Bitmap screenBitmap;

    public CaptureForm2()
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
        this.DoubleBuffered = true;
        this.TopMost = true;
        this.ShowInTaskbar = false;
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new System.Drawing.Point(0, 0);

        screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        using (Graphics g = Graphics.FromImage(screenBitmap))
        {
            g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, screenBitmap.Size);
        }

        this.Paint += CaptureForm_Paint;
        this.MouseDown += CaptureForm_MouseDown;
        this.MouseMove += CaptureForm_MouseMove;
        this.MouseUp += CaptureForm_MouseUp;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (isDragging)
        {
            selectionRect = new Rectangle(
                Math.Min(startPoint.X, endPoint.X),
                Math.Min(startPoint.Y, endPoint.Y),
                Math.Abs(endPoint.X - startPoint.X),
                Math.Abs(endPoint.Y - startPoint.Y)
            );

            using (Pen pen = new Pen(Color.Red, 2))
            {
                e.Graphics.DrawRectangle(pen, selectionRect);
            }
        }

        e.Graphics.DrawImage(screenBitmap, System.Drawing.Point.Empty);
    }

    private void CaptureForm_Paint(object sender, PaintEventArgs e)
    {
        OnPaint(e);
    }

    private void CaptureForm_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            startPoint = e.Location;
            isDragging = true;
            this.Invalidate();
        }
    }

    private void CaptureForm_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            endPoint = e.Location;
            this.Invalidate();
        }
    }

    private void CaptureForm_MouseUp(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            endPoint = e.Location;
            isDragging = false;

            selectionRect = new Rectangle(
                Math.Min(startPoint.X, endPoint.X),
                Math.Min(startPoint.Y, endPoint.Y),
                Math.Abs(endPoint.X - startPoint.X),
                Math.Abs(endPoint.Y - startPoint.Y)
            );

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }

    public Mat GetSelectedRegion()
    {
        if (selectionRect.Width > 0 && selectionRect.Height > 0)
        {
            using (Bitmap selectedBitmap = screenBitmap.Clone(selectionRect, screenBitmap.PixelFormat))
            {
                // 将 Bitmap 转换为 byte 数组
                using (MemoryStream ms = new MemoryStream())
                {
                    selectedBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteArray = ms.ToArray();

                    // 将 byte 数组转换为 Mat
                    Mat mat = Cv2.ImDecode(byteArray, ImreadModes.Color);
                    return mat;
                }
            }
        }

        return null;
    }
}